package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IMemoEmpDAO;
import org.jsola.hr.dto.MemoEmpAddDTO;
import org.jsola.hr.dto.MemoEmpUpdateDTO;
import org.jsola.hr.entity.MemoEmpDO;
import org.jsola.hr.query.MemoEmpQuery;
import org.jsola.hr.service.IMemoEmpService;
import org.jsola.hr.vo.MemoEmpListVO;
import org.jsola.hr.vo.MemoEmpVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 备忘录相关员工
 *
 * @author zhr
 */
@Service("hrMemoEmpServiceImpl")
public class MemoEmpServiceImpl implements IMemoEmpService {

    @Autowired
    private IMemoEmpDAO memoEmpDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public MemoEmpVO save(MemoEmpAddDTO memoEmpAddDTO, TokenUser tokenUser) {
        // 转DO
        MemoEmpDO memoEmpDO = memoEmpAddDTO.to(MemoEmpDO.class);
        // 保存
        memoEmpDO = save(memoEmpDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return memoEmpDO.to(MemoEmpVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(MemoEmpUpdateDTO memoEmpUpdateDTO, TokenUser tokenUser) {
        //转DO
        MemoEmpDO memoEmpDO = memoEmpUpdateDTO.to(MemoEmpDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(memoEmpDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... memoEmpIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) memoEmpIds);
    }


    @Override
    public MemoEmpVO selectById(String memoEmpId, String siteId) {
        MemoEmpDO memoEmpDO = selectDOById(memoEmpId, siteId);
        if (memoEmpDO == null) {
            return null;
        }
        return memoEmpDO.to(MemoEmpVO.class);
    }

    @Override
    public List<MemoEmpListVO> select(MemoEmpQuery memoEmpQuery, String siteId) {
        List<MemoEmpDO> memoEmpDOList = selectDO(memoEmpQuery, siteId);
        if(CollectionUtils.isEmpty(memoEmpDOList)) {
            return memoEmpDOList == null ? null : new ArrayList<>();
        }
        return memoEmpDOList.stream()
                .map(memoEmpDO -> memoEmpDO.to(MemoEmpListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(MemoEmpQuery memoEmpQuery, String siteId) {
        Example example = buildExample(memoEmpQuery,siteId);
        return memoEmpDAO.selectCountByExample(example);
    }

    @Override
    public Page<MemoEmpListVO> selectPage(MemoEmpQuery memoEmpQuery, String siteId) {
        Example example = buildExample(memoEmpQuery,siteId);
        Page<MemoEmpDO> page = memoEmpDAO.selectPageByExample(example,
                memoEmpQuery.getPageNo(),
                memoEmpQuery.getPageSize());

        return page.to(MemoEmpListVO.class);
    }

    @Override
    public MemoEmpDO selectDOById(String memoEmpId, String siteId) {
        return listById(memoEmpId, siteId);
    }

    @Override
    public List<MemoEmpDO> selectDO(MemoEmpQuery memoEmpQuery, String siteId) {
        Example example = buildExample(memoEmpQuery,siteId);
        return memoEmpDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param memoEmpQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(MemoEmpQuery memoEmpQuery, String siteId) {
        Example example = new Example(MemoEmpDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (memoEmpQuery.getMemoId() != null) {
            example.and().andEqualTo("memoId", memoEmpQuery.getMemoId());
        }
        if (memoEmpQuery.getEmpId() != null) {
            example.and().andEqualTo("empId", memoEmpQuery.getEmpId());
        }
        if (memoEmpQuery.getEmpName() != null) {
            example.and().andEqualTo("empName", memoEmpQuery.getEmpName());
        }
        // 排序
        ExampleKit.setExampleOrder(example,memoEmpQuery.getOrders());
        return example;
    }
}




